﻿using System;
using System.Diagnostics;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Windows;

namespace 获取网卡序列号_MD5_
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// Computer Information 
        public class Computer
        {
            public string CpuID; //1.cpu序列号
            public string MacAddress; //2.mac序列号
            public string DiskID; //3.硬盘id
            public string IpAddress; //4.ip地址
            public string LoginUserName; //5.登录用户名
            public string ComputerName; //6.计算机名
            public string SystemType; //7.系统类型
            public string TotalPhysicalMemory; //8.内存量 单位：M

            public Computer()
            {
                CpuID = GetCpuID();
                MacAddress = GetMacAddress();
                DiskID = GetDiskID();
                IpAddress = GetIPAddress();
                TotalPhysicalMemory = GetTotalPhysicalMemory();
            }
            //1.获取CPU序列号代码 

            string GetCpuID()
            {
                try
                {
                    string cpuInfo = "";//cpu序列号 
                    ManagementClass mc = new ManagementClass("Win32_Processor");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    }
                    moc = null;
                    mc = null;
                    return cpuInfo;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }

            //2.获取网卡硬件地址 

            string GetMacAddress()
            {
                try
                {
                    string mac = "";
                    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            mac = mo["MacAddress"].ToString();
                            break;
                        }
                    }
                    moc = null;
                    mc = null;
                    return mac;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }

            //3.获取硬盘ID 
            string GetDiskID()
            {
                try
                {
                    String HDid = "";
                    ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        HDid = (string)mo.Properties["Model"].Value;
                    }
                    moc = null;
                    mc = null;
                    return HDid;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }


            //4.获取IP地址 

            string GetIPAddress()
            {
                try
                {
                    string st = "";
                    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            //st=mo["IpAddress"].ToString(); 
                            System.Array ar;
                            ar = (System.Array)(mo.Properties["IpAddress"].Value);
                            st = ar.GetValue(0).ToString();
                            break;
                        }
                    }
                    moc = null;
                    mc = null;
                    return st;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }

            /// 8.物理内存 
            string GetTotalPhysicalMemory()
            {
                try
                {

                    string st = "";
                    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {

                        st = mo["TotalPhysicalMemory"].ToString();

                    }
                    moc = null;
                    mc = null;
                    return st;
                }
                catch
                {
                    return "";
                }
            }
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await Dispatcher.InvokeAsync(() =>
            {
                foreach (System.Net.NetworkInformation.NetworkInterface netInterface in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
                {
                    // 获取网卡的 MAC 地址
                    System.Net.NetworkInformation.PhysicalAddress macAddress = netInterface.GetPhysicalAddress();
                    Debug.WriteLine($"MAC 地址: {macAddress}");
                }
                // 将 MAC 地址转换为字节数组
                // byte[] macBytes = StringToByteArray(new Computer().MacAddress);

                // 计算 MD5
                using (MD5 md5 = MD5.Create())
                {
                    string macaddress = new Computer().MacAddress;
                    // 计算 MD5 哈希
                    string md5Hash = GetMd5Hash(macaddress);
                    // 输出结果
                    Debug.WriteLine($"MAC 地址: {macaddress}");
                    Debug.WriteLine($"MD5: {md5Hash}");
                }

            });
        }

        // 计算 MD5 哈希值的方法
        public static string GetMd5Hash(string input)
        {
            // 创建 MD5 对象
            using (MD5 md5 = MD5.Create())
            {
                // 将字符串转换为字节数组
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);

                // 计算 MD5 哈希值
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                // 将字节数组转换为 16 进制字符串
                StringBuilder sb = new StringBuilder();
                foreach (byte b in hashBytes)
                {
                    sb.Append(b.ToString("x2")); // 转为 16 进制并小写
                }
                return sb.ToString();
            }
        }
    }
}
